<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li class="active"><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. HTTP的架构模式">1. HTTP的架构模式</a><ul><li><a href="#t11.1. HTTP的特点">1.1. HTTP的特点</a></li></ul></li><li><a href="#t22. 双向通信">2. 双向通信</a><ul><li><a href="#t32.1 轮询">2.1 轮询</a></li><li><a href="#t41.2 长轮询">1.2 长轮询</a></li><li><a href="#t51.3 iframe流">1.3 iframe流</a></li></ul></li><li><a href="#t62.websocket">2.websocket</a><ul><li><a href="#t72.1 websocket 优势">2.1 websocket 优势</a></li><li><a href="#t82.2 websocket实战">2.2 websocket实战</a><ul><li><a href="#t92.2.1 服务端">2.2.1 服务端</a></li><li><a href="#t102.2.2 客户端">2.2.2 客户端</a></li></ul></li><li><a href="#t112.3 如何建立连接">2.3 如何建立连接</a><ul><li><a href="#t122.3.1 客户端：申请协议升级">2.3.1 客户端：申请协议升级</a></li><li><a href="#t132.3.2 服务端：响应协议升级">2.3.2 服务端：响应协议升级</a></li><li><a href="#t142.3.3 Sec-WebSocket-Accept的计算">2.3.3 Sec-WebSocket-Accept的计算</a></li><li><a href="#t152.3.4  Sec-WebSocket-Key/Accept的作用">2.3.4  Sec-WebSocket-Key/Accept的作用</a></li></ul></li><li><a href="#t162.4 数据帧格式">2.4 数据帧格式</a><ul><li><a href="#t172.4.1 数据帧格式">2.4.1 数据帧格式</a></li><li><a href="#t182.4.2 掩码算法">2.4.2 掩码算法</a></li><li><a href="#t192.4.3 服务器实战">2.4.3 服务器实战</a></li></ul></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. HTTP&#x7684;&#x67B6;&#x6784;&#x6A21;&#x5F0F;">1. HTTP&#x7684;&#x67B6;&#x6784;&#x6A21;&#x5F0F; <a href="#t01. HTTP&#x7684;&#x67B6;&#x6784;&#x6A21;&#x5F0F;"> # </a></h2>
<p>Http&#x662F;&#x5BA2;&#x6237;&#x7AEF;/&#x670D;&#x52A1;&#x5668;&#x6A21;&#x5F0F;&#x4E2D;&#x8BF7;&#x6C42;-&#x54CD;&#x5E94;&#x6240;&#x7528;&#x7684;&#x534F;&#x8BAE;&#xFF0C;&#x5728;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;(&#x4E00;&#x822C;&#x662F;web&#x6D4F;&#x89C8;&#x5668;)&#x5411;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4EA4;HTTP&#x8BF7;&#x6C42;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x8BF7;&#x6C42;&#x7684;&#x8D44;&#x6E90;</p>
<h3 id="t11.1. HTTP&#x7684;&#x7279;&#x70B9;">1.1. HTTP&#x7684;&#x7279;&#x70B9; <a href="#t11.1. HTTP&#x7684;&#x7279;&#x70B9;"> # </a></h3>
<ul>
<li>HTTP&#x662F;&#x534A;&#x53CC;&#x5DE5;&#x534F;&#x8BAE;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5728;&#x540C;&#x4E00;&#x65F6;&#x523B;&#x6570;&#x636E;&#x53EA;&#x80FD;&#x5355;&#x5411;&#x6D41;&#x52A8;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x8BF7;&#x6C42;(&#x5355;&#x5411;&#x7684;)&#xFF0C;&#x7136;&#x540E;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x8BF7;&#x6C42;(&#x5355;&#x5411;&#x7684;)&#x3002;</li>
<li>&#x670D;&#x52A1;&#x5668;&#x4E0D;&#x80FD;&#x4E3B;&#x52A8;&#x63A8;&#x9001;&#x6570;&#x636E;&#x7ED9;&#x6D4F;&#x89C8;&#x5668;&#x3002;</li>
</ul>
<h2 id="t22. &#x53CC;&#x5411;&#x901A;&#x4FE1;">2. &#x53CC;&#x5411;&#x901A;&#x4FE1; <a href="#t22. &#x53CC;&#x5411;&#x901A;&#x4FE1;"> # </a></h2>
<p>Comet&#x662F;&#x4E00;&#x79CD;&#x7528;&#x4E8E;web&#x7684;&#x63A8;&#x9001;&#x6280;&#x672F;&#xFF0C;&#x80FD;&#x4F7F;&#x670D;&#x52A1;&#x5668;&#x80FD;&#x5B9E;&#x65F6;&#x5730;&#x5C06;&#x66F4;&#x65B0;&#x7684;&#x4FE1;&#x606F;&#x4F20;&#x9001;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x800C;&#x65E0;&#x987B;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#xFF0C;&#x76EE;&#x524D;&#x6709;&#x4E09;&#x79CD;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;:&#x8F6E;&#x8BE2;&#xFF08;polling&#xFF09; &#x957F;&#x8F6E;&#x8BE2;&#xFF08;long-polling&#xFF09;&#x548C;iframe&#x6D41;&#xFF08;streaming&#xFF09;&#x3002;</p>
<h3 id="t32.1 &#x8F6E;&#x8BE2;">2.1 &#x8F6E;&#x8BE2; <a href="#t32.1 &#x8F6E;&#x8BE2;"> # </a></h3>
<ul>
<li>&#x8F6E;&#x8BE2;&#x662F;&#x5BA2;&#x6237;&#x7AEF;&#x548C;&#x670D;&#x52A1;&#x5668;&#x4E4B;&#x95F4;&#x4F1A;&#x4E00;&#x76F4;&#x8FDB;&#x884C;&#x8FDE;&#x63A5;&#xFF0C;&#x6BCF;&#x9694;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x5C31;&#x8BE2;&#x95EE;&#x4E00;&#x6B21;</li>
<li>&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x8FDE;&#x63A5;&#x6570;&#x4F1A;&#x5F88;&#x591A;&#xFF0C;&#x4E00;&#x4E2A;&#x63A5;&#x53D7;&#xFF0C;&#x4E00;&#x4E2A;&#x53D1;&#x9001;&#x3002;&#x800C;&#x4E14;&#x6BCF;&#x6B21;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x90FD;&#x4F1A;&#x6709;Http&#x7684;Header&#xFF0C;&#x4F1A;&#x5F88;&#x8017;&#x6D41;&#x91CF;&#xFF0C;&#x4E5F;&#x4F1A;&#x6D88;&#x8017;CPU&#x7684;&#x5229;&#x7528;&#x7387;</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/polling.jpg" alt="polling"></p>
<p>server.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">let</span> app = express();
app.use(express.static(__dirname));
app.use(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res,next</span>)</span>{
    res.header(<span class="hljs-string">&apos;Access-Control-Allow-Origin&apos;</span>, <span class="hljs-string">&apos;http://localhost:8000&apos;</span>);
    res.end(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().toLocaleTimeString());
});
app.listen(<span class="hljs-number">8080</span>);
</code></pre>
<pre><code class="lang-js">&lt;body&gt;
    &lt;div id=&quot;clock&quot;&gt;&lt;/div&gt;
    &lt;script&gt;
            setInterval(function () {
                let xhr = new XMLHttpRequest();
                xhr.open(&apos;GET&apos;, &apos;http://localhost:8080&apos;, true);
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) {
                        document.querySelector(&apos;#clock&apos;).innerHTML = xhr.responseText;
                    }
                }
                xhr.send();
            }, 1000);
    &lt;/script&gt;
&lt;/body&gt;
</code></pre>
<h3 id="t41.2 &#x957F;&#x8F6E;&#x8BE2;">1.2 &#x957F;&#x8F6E;&#x8BE2; <a href="#t41.2 &#x957F;&#x8F6E;&#x8BE2;"> # </a></h3>
<ul>
<li>&#x957F;&#x8F6E;&#x8BE2;&#x662F;&#x5BF9;&#x8F6E;&#x8BE2;&#x7684;&#x6539;&#x8FDB;&#x7248;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;HTTP&#x7ED9;&#x670D;&#x52A1;&#x5668;&#x4E4B;&#x540E;&#xFF0C;&#x770B;&#x6709;&#x6CA1;&#x6709;&#x65B0;&#x6D88;&#x606F;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x65B0;&#x6D88;&#x606F;&#xFF0C;&#x5C31;&#x4E00;&#x76F4;&#x7B49;&#x5F85;</li>
<li>&#x5F53;&#x6709;&#x65B0;&#x6D88;&#x606F;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x624D;&#x4F1A;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x5728;&#x67D0;&#x79CD;&#x7A0B;&#x5EA6;&#x4E0A;&#x51CF;&#x5C0F;&#x4E86;&#x7F51;&#x7EDC;&#x5E26;&#x5BBD;&#x548C;CPU&#x5229;&#x7528;&#x7387;&#x7B49;&#x95EE;&#x9898;&#x3002;</li>
<li>&#x7531;&#x4E8E;http&#x6570;&#x636E;&#x5305;&#x7684;&#x5934;&#x90E8;&#x6570;&#x636E;&#x91CF;&#x5F80;&#x5F80;&#x5F88;&#x5927;&#xFF08;&#x901A;&#x5E38;&#x6709;400&#x591A;&#x4E2A;&#x5B57;&#x8282;&#xFF09;&#xFF0C;&#x4F46;&#x662F;&#x771F;&#x6B63;&#x88AB;&#x670D;&#x52A1;&#x5668;&#x9700;&#x8981;&#x7684;&#x6570;&#x636E;&#x5374;&#x5F88;&#x5C11;&#xFF08;&#x6709;&#x65F6;&#x53EA;&#x6709;10&#x4E2A;&#x5B57;&#x8282;&#x5DE6;&#x53F3;&#xFF09;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x6570;&#x636E;&#x5305;&#x5728;&#x7F51;&#x7EDC;&#x4E0A;&#x5468;&#x671F;&#x6027;&#x7684;&#x4F20;&#x8F93;&#xFF0C;&#x96BE;&#x514D;&#x5BF9;&#x7F51;&#x7EDC;&#x5E26;&#x5BBD;&#x662F;&#x4E00;&#x79CD;&#x6D6A;&#x8D39;</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/longpolling.png" alt="longpolling"></p>
<p>clock.html</p>
<pre><code class="lang-js">  &lt;div id=<span class="hljs-string">&quot;clock&quot;</span>&gt;<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    &lt;script&gt;
        (<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">poll</span>(<span class="hljs-params"></span>) </span>{
                <span class="hljs-keyword">let</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
                xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;http://localhost:8080&apos;</span>, <span class="hljs-literal">true</span>);
                xhr.onreadystatechange = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
                    <span class="hljs-keyword">if</span> (xhr.readyState == <span class="hljs-number">4</span> &amp;&amp; xhr.status == <span class="hljs-number">200</span>) {
                        <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#clock&apos;</span>).innerHTML = xhr.responseText;
                        poll();
                    }
                }
                xhr.send();
        })();
    <span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<blockquote>
<p>long poll &#x9700;&#x8981;&#x6709;&#x5F88;&#x9AD8;&#x7684;&#x5E76;&#x53D1;&#x80FD;&#x529B;</p>
</blockquote>
<h3 id="t51.3 iframe&#x6D41;">1.3 iframe&#x6D41; <a href="#t51.3 iframe&#x6D41;"> # </a></h3>
<ul>
<li>&#x901A;&#x8FC7;&#x5728;HTML&#x9875;&#x9762;&#x91CC;&#x5D4C;&#x5165;&#x4E00;&#x4E2A;&#x9690;&#x85CF;&#x7684;iframe,&#x7136;&#x540E;&#x5C06;&#x8FD9;&#x4E2A;iframe&#x7684;src&#x5C5E;&#x6027;&#x8BBE;&#x4E3A;&#x5BF9;&#x4E00;&#x4E2A;&#x957F;&#x8FDE;&#x63A5;&#x7684;&#x8BF7;&#x6C42;,&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5C31;&#x80FD;&#x6E90;&#x6E90;&#x4E0D;&#x65AD;&#x5730;&#x5F80;&#x5BA2;&#x6237;&#x63A8;&#x9001;&#x6570;&#x636E;&#x3002;</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/iframeflow.png" alt="iframeflow"></p>
<p>server.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">const</span> app = express();
app.use(express.static(__dirname));
app.get(<span class="hljs-string">&apos;/clock&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    setInterval(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        res.write(<span class="hljs-string">`
            &lt;script type=&quot;text/javascript&quot;&gt;
                parent.document.getElementById(&apos;clock&apos;).innerHTML = &quot;<span class="hljs-subst">${<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().toLocaleTimeString()}</span>&quot;;
            &lt;/script&gt;
        `</span>);
    }, <span class="hljs-number">1000</span>);
});
app.listen(<span class="hljs-number">8080</span>);
</code></pre>
<p>client.html</p>
<pre><code class="lang-html">    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;clock&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">iframe</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;/clock&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">&quot; display:none&quot;</span> /&gt;</span>
</code></pre>
<h2 id="t62.websocket">2.websocket <a href="#t62.websocket"> # </a></h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API">WebSockets_API</a> &#x89C4;&#x8303;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A; API &#x7528;&#x4EE5;&#x5728;&#x7F51;&#x9875;&#x6D4F;&#x89C8;&#x5668;&#x548C;&#x670D;&#x52A1;&#x5668;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A; socket &#x8FDE;&#x63A5;&#x3002;&#x901A;&#x4FD7;&#x5730;&#x8BB2;&#xFF1A;&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x548C;&#x670D;&#x52A1;&#x5668;&#x4FDD;&#x6709;&#x4E00;&#x4E2A;&#x6301;&#x4E45;&#x7684;&#x8FDE;&#x63A5;&#xFF0C;&#x4E24;&#x8FB9;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x610F;&#x65F6;&#x95F4;&#x5F00;&#x59CB;&#x53D1;&#x9001;&#x6570;&#x636E;&#x3002;</li>
<li>HTML5&#x5F00;&#x59CB;&#x63D0;&#x4F9B;&#x7684;&#x4E00;&#x79CD;&#x6D4F;&#x89C8;&#x5668;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x5168;&#x53CC;&#x5DE5;&#x901A;&#x8BAF;&#x7684;&#x7F51;&#x7EDC;&#x6280;&#x672F;</li>
<li>&#x5C5E;&#x4E8E;&#x5E94;&#x7528;&#x5C42;&#x534F;&#x8BAE;&#xFF0C;&#x5B83;&#x57FA;&#x4E8E;TCP&#x4F20;&#x8F93;&#x534F;&#x8BAE;&#xFF0C;&#x5E76;&#x590D;&#x7528;HTTP&#x7684;&#x63E1;&#x624B;&#x901A;&#x9053;&#x3002;</li>
</ul>
<h3 id="t72.1 websocket &#x4F18;&#x52BF;">2.1 websocket &#x4F18;&#x52BF; <a href="#t72.1 websocket &#x4F18;&#x52BF;"> # </a></h3>
<ul>
<li>&#x652F;&#x6301;&#x53CC;&#x5411;&#x901A;&#x4FE1;&#xFF0C;&#x5B9E;&#x65F6;&#x6027;&#x66F4;&#x5F3A;&#x3002;</li>
<li>&#x66F4;&#x597D;&#x7684;&#x4E8C;&#x8FDB;&#x5236;&#x652F;&#x6301;&#x3002;</li>
<li>&#x8F83;&#x5C11;&#x7684;&#x63A7;&#x5236;&#x5F00;&#x9500;&#x3002;&#x8FDE;&#x63A5;&#x521B;&#x5EFA;&#x540E;&#xFF0C;ws&#x5BA2;&#x6237;&#x7AEF;&#x3001;&#x670D;&#x52A1;&#x7AEF;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x4EA4;&#x6362;&#x65F6;&#xFF0C;&#x534F;&#x8BAE;&#x63A7;&#x5236;&#x7684;&#x6570;&#x636E;&#x5305;&#x5934;&#x90E8;&#x8F83;&#x5C0F;&#x3002;</li>
</ul>
<h3 id="t82.2 websocket&#x5B9E;&#x6218;">2.2 websocket&#x5B9E;&#x6218; <a href="#t82.2 websocket&#x5B9E;&#x6218;"> # </a></h3>
<h4 id="t92.2.1 &#x670D;&#x52A1;&#x7AEF;">2.2.1 &#x670D;&#x52A1;&#x7AEF; <a href="#t92.2.1 &#x670D;&#x52A1;&#x7AEF;"> # </a></h4>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> app = express();
<span class="hljs-keyword">let</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>).createServer(app);
app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    res.sendFile(path.resolve(__dirname, <span class="hljs-string">&apos;index.html&apos;</span>));
});
app.listen(<span class="hljs-number">3000</span>);


<span class="hljs-comment">//-----------------------------------------------</span>
<span class="hljs-keyword">let</span> WebSocketServer = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;ws&apos;</span>).Server;
<span class="hljs-keyword">let</span> wsServer = <span class="hljs-keyword">new</span> WebSocketServer({ <span class="hljs-attr">port</span>: <span class="hljs-number">8888</span> });
wsServer.on(<span class="hljs-string">&apos;connection&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FDE;&#x63A5;&#x6210;&#x529F;&apos;</span>);
    socket.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">message</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x63A5;&#x6536;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x6D88;&#x606F;:&apos;</span> + message);
        socket.send(<span class="hljs-string">&apos;&#x670D;&#x52A1;&#x5668;&#x56DE;&#x5E94;:&apos;</span> + message);
    });
});
</code></pre>
<h4 id="t102.2.2 &#x5BA2;&#x6237;&#x7AEF;">2.2.2 &#x5BA2;&#x6237;&#x7AEF; <a href="#t102.2.2 &#x5BA2;&#x6237;&#x7AEF;"> # </a></h4>
<pre><code class="lang-html">    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">let</span> ws = <span class="hljs-keyword">new</span> WebSocket(<span class="hljs-string">&apos;ws://localhost:8888&apos;</span>);
        ws.onopen = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x6210;&#x529F;&apos;</span>);
            ws.send(<span class="hljs-string">&apos;hello&apos;</span>);
        }
        ws.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x6536;&#x5230;&#x670D;&#x52A1;&#x5668;&#x7684;&#x54CD;&#x5E94; &apos;</span> + event.data);
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<h3 id="t112.3 &#x5982;&#x4F55;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;">2.3 &#x5982;&#x4F55;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5; <a href="#t112.3 &#x5982;&#x4F55;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;"> # </a></h3>
<p>WebSocket&#x590D;&#x7528;&#x4E86;HTTP&#x7684;&#x63E1;&#x624B;&#x901A;&#x9053;&#x3002;&#x5177;&#x4F53;&#x6307;&#x7684;&#x662F;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x901A;&#x8FC7;HTTP&#x8BF7;&#x6C42;&#x4E0E;WebSocket&#x670D;&#x52A1;&#x7AEF;&#x534F;&#x5546;&#x5347;&#x7EA7;&#x534F;&#x8BAE;&#x3002;&#x534F;&#x8BAE;&#x5347;&#x7EA7;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x540E;&#x7EED;&#x7684;&#x6570;&#x636E;&#x4EA4;&#x6362;&#x5219;&#x9075;&#x7167;WebSocket&#x7684;&#x534F;&#x8BAE;&#x3002;</p>
<h4 id="t122.3.1 &#x5BA2;&#x6237;&#x7AEF;&#xFF1A;&#x7533;&#x8BF7;&#x534F;&#x8BAE;&#x5347;&#x7EA7;">2.3.1 &#x5BA2;&#x6237;&#x7AEF;&#xFF1A;&#x7533;&#x8BF7;&#x534F;&#x8BAE;&#x5347;&#x7EA7; <a href="#t122.3.1 &#x5BA2;&#x6237;&#x7AEF;&#xFF1A;&#x7533;&#x8BF7;&#x534F;&#x8BAE;&#x5347;&#x7EA7;"> # </a></h4>
<p>&#x9996;&#x5148;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x8D77;&#x534F;&#x8BAE;&#x5347;&#x7EA7;&#x8BF7;&#x6C42;&#x3002;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x91C7;&#x7528;&#x7684;&#x662F;&#x6807;&#x51C6;&#x7684;HTTP&#x62A5;&#x6587;&#x683C;&#x5F0F;&#xFF0C;&#x4E14;&#x53EA;&#x652F;&#x6301;GET&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-js">GET ws:<span class="hljs-comment">//localhost:8888/ HTTP/1.1</span>
Host: localhost:<span class="hljs-number">8888</span>
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: <span class="hljs-number">13</span>
Sec-WebSocket-Key: IHfMdf8a0aQXbwQO1pkGdA==
</code></pre>
<ul>
<li>Connection: Upgrade&#xFF1A;&#x8868;&#x793A;&#x8981;&#x5347;&#x7EA7;&#x534F;&#x8BAE;</li>
<li>Upgrade: websocket&#xFF1A;&#x8868;&#x793A;&#x8981;&#x5347;&#x7EA7;&#x5230;websocket&#x534F;&#x8BAE;</li>
<li>Sec-WebSocket-Version: 13&#xFF1A;&#x8868;&#x793A;websocket&#x7684;&#x7248;&#x672C;</li>
<li>Sec-WebSocket-Key&#xFF1A;&#x4E0E;&#x540E;&#x9762;&#x670D;&#x52A1;&#x7AEF;&#x54CD;&#x5E94;&#x9996;&#x90E8;&#x7684;Sec-WebSocket-Accept&#x662F;&#x914D;&#x5957;&#x7684;&#xFF0C;&#x63D0;&#x4F9B;&#x57FA;&#x672C;&#x7684;&#x9632;&#x62A4;&#xFF0C;&#x6BD4;&#x5982;&#x6076;&#x610F;&#x7684;&#x8FDE;&#x63A5;&#xFF0C;&#x6216;&#x8005;&#x65E0;&#x610F;&#x7684;&#x8FDE;&#x63A5;&#x3002;</li>
</ul>
<h4 id="t132.3.2 &#x670D;&#x52A1;&#x7AEF;&#xFF1A;&#x54CD;&#x5E94;&#x534F;&#x8BAE;&#x5347;&#x7EA7;">2.3.2 &#x670D;&#x52A1;&#x7AEF;&#xFF1A;&#x54CD;&#x5E94;&#x534F;&#x8BAE;&#x5347;&#x7EA7; <a href="#t132.3.2 &#x670D;&#x52A1;&#x7AEF;&#xFF1A;&#x54CD;&#x5E94;&#x534F;&#x8BAE;&#x5347;&#x7EA7;"> # </a></h4>
<p>&#x670D;&#x52A1;&#x7AEF;&#x8FD4;&#x56DE;&#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF0C;&#x72B6;&#x6001;&#x4EE3;&#x7801;101&#x8868;&#x793A;&#x534F;&#x8BAE;&#x5207;&#x6362;&#x3002;&#x5230;&#x6B64;&#x5B8C;&#x6210;&#x534F;&#x8BAE;&#x5347;&#x7EA7;&#xFF0C;&#x540E;&#x7EED;&#x7684;&#x6570;&#x636E;&#x4EA4;&#x4E92;&#x90FD;&#x6309;&#x7167;&#x65B0;&#x7684;&#x534F;&#x8BAE;&#x6765;&#x3002;</p>
<pre><code class="lang-js">HTTP/<span class="hljs-number">1.1</span> <span class="hljs-number">101</span> Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: aWAY+V/uyz5ILZEoWuWdxjnlb7E=



</code></pre>
<h4 id="t142.3.3 Sec-WebSocket-Accept&#x7684;&#x8BA1;&#x7B97;">2.3.3 Sec-WebSocket-Accept&#x7684;&#x8BA1;&#x7B97; <a href="#t142.3.3 Sec-WebSocket-Accept&#x7684;&#x8BA1;&#x7B97;"> # </a></h4>
<p>Sec-WebSocket-Accept&#x6839;&#x636E;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x9996;&#x90E8;&#x7684;Sec-WebSocket-Key&#x8BA1;&#x7B97;&#x51FA;&#x6765;&#x3002;
&#x8BA1;&#x7B97;&#x516C;&#x5F0F;&#x4E3A;&#xFF1A;</p>
<ul>
<li>&#x5C06;Sec-WebSocket-Key&#x8DDF;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&#x62FC;&#x63A5;&#x3002;</li>
<li>&#x901A;&#x8FC7;SHA1&#x8BA1;&#x7B97;&#x51FA;&#x6458;&#x8981;&#xFF0C;&#x5E76;&#x8F6C;&#x6210;base64&#x5B57;&#x7B26;&#x4E32;<pre><code class="lang-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>);
<span class="hljs-keyword">const</span> number = <span class="hljs-string">&apos;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&apos;</span>;
<span class="hljs-keyword">const</span> webSocketKey = <span class="hljs-string">&apos;IHfMdf8a0aQXbwQO1pkGdA==&apos;</span>;
<span class="hljs-keyword">let</span> websocketAccept = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>).createHash(<span class="hljs-string">&apos;sha1&apos;</span>).update(webSocketKey + number).digest(<span class="hljs-string">&apos;base64&apos;</span>);
<span class="hljs-built_in">console</span>.log(websocketAccept);<span class="hljs-comment">//aWAY+V/uyz5ILZEoWuWdxjnlb7E=</span>
</code></pre>
</li>
</ul>
<h4 id="t152.3.4  Sec-WebSocket-Key/Accept&#x7684;&#x4F5C;&#x7528;">2.3.4  Sec-WebSocket-Key/Accept&#x7684;&#x4F5C;&#x7528; <a href="#t152.3.4  Sec-WebSocket-Key/Accept&#x7684;&#x4F5C;&#x7528;"> # </a></h4>
<ul>
<li>&#x907F;&#x514D;&#x670D;&#x52A1;&#x7AEF;&#x6536;&#x5230;&#x975E;&#x6CD5;&#x7684;websocket&#x8FDE;&#x63A5;</li>
<li>&#x786E;&#x4FDD;&#x670D;&#x52A1;&#x7AEF;&#x7406;&#x89E3;websocket&#x8FDE;&#x63A5;</li>
<li>&#x7528;&#x6D4F;&#x89C8;&#x5668;&#x91CC;&#x53D1;&#x8D77;ajax&#x8BF7;&#x6C42;&#xFF0C;&#x8BBE;&#x7F6E;header&#x65F6;&#xFF0C;Sec-WebSocket-Key&#x4EE5;&#x53CA;&#x5176;&#x4ED6;&#x76F8;&#x5173;&#x7684;header&#x662F;&#x88AB;&#x7981;&#x6B62;&#x7684;</li>
<li>Sec-WebSocket-Key&#x4E3B;&#x8981;&#x76EE;&#x7684;&#x5E76;&#x4E0D;&#x662F;&#x786E;&#x4FDD;&#x6570;&#x636E;&#x7684;&#x5B89;&#x5168;&#x6027;&#xFF0C;&#x56E0;&#x4E3A;Sec-WebSocket-Key&#x3001;Sec-WebSocket-Accept&#x7684;&#x8F6C;&#x6362;&#x8BA1;&#x7B97;&#x516C;&#x5F0F;&#x662F;&#x516C;&#x5F00;&#x7684;&#xFF0C;&#x800C;&#x4E14;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x6700;&#x4E3B;&#x8981;&#x7684;&#x4F5C;&#x7528;&#x662F;&#x9884;&#x9632;&#x4E00;&#x4E9B;&#x5E38;&#x89C1;&#x7684;&#x610F;&#x5916;&#x60C5;&#x51B5;&#xFF08;&#x975E;&#x6545;&#x610F;&#x7684;&#xFF09;</li>
</ul>
<h3 id="t162.4 &#x6570;&#x636E;&#x5E27;&#x683C;&#x5F0F;">2.4 &#x6570;&#x636E;&#x5E27;&#x683C;&#x5F0F; <a href="#t162.4 &#x6570;&#x636E;&#x5E27;&#x683C;&#x5F0F;"> # </a></h3>
<p>WebSocket&#x5BA2;&#x6237;&#x7AEF;&#x3001;&#x670D;&#x52A1;&#x7AEF;&#x901A;&#x4FE1;&#x7684;&#x6700;&#x5C0F;&#x5355;&#x4F4D;&#x662F;<a href="https://tools.ietf.org/html/rfc6455#section-5.2">&#x5E27;</a>&#xFF0C;&#x7531;1&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5E27;&#x7EC4;&#x6210;&#x4E00;&#x6761;&#x5B8C;&#x6574;&#x7684;&#x6D88;&#x606F;&#xFF08;message&#xFF09;&#x3002;</p>
<ul>
<li>&#x53D1;&#x9001;&#x7AEF;&#xFF1A;&#x5C06;&#x6D88;&#x606F;&#x5207;&#x5272;&#x6210;&#x591A;&#x4E2A;&#x5E27;&#xFF0C;&#x5E76;&#x53D1;&#x9001;&#x7ED9;&#x670D;&#x52A1;&#x7AEF;</li>
<li>&#x63A5;&#x6536;&#x7AEF;&#xFF1A;&#x63A5;&#x6536;&#x6D88;&#x606F;&#x5E27;&#xFF0C;&#x5E76;&#x5C06;&#x5173;&#x8054;&#x7684;&#x5E27;&#x91CD;&#x65B0;&#x7EC4;&#x88C5;&#x6210;&#x5B8C;&#x6574;&#x7684;&#x6D88;&#x606F;</li>
</ul>
<h4 id="t172.4.1 &#x6570;&#x636E;&#x5E27;&#x683C;&#x5F0F;">2.4.1 &#x6570;&#x636E;&#x5E27;&#x683C;&#x5F0F; <a href="#t172.4.1 &#x6570;&#x636E;&#x5E27;&#x683C;&#x5F0F;"> # </a></h4>
<p>&#x5355;&#x4F4D;&#x662F;&#x6BD4;&#x7279;&#x3002;&#x6BD4;&#x5982;FIN&#x3001;RSV1&#x5404;&#x5360;&#x636E;1&#x6BD4;&#x7279;&#xFF0C;opcode&#x5360;&#x636E;4&#x6BD4;&#x7279;</p>
<pre><code class="lang-js">  <span class="hljs-number">0</span>                   <span class="hljs-number">1</span>                   <span class="hljs-number">2</span>                   <span class="hljs-number">3</span>
  <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> <span class="hljs-number">6</span> <span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> <span class="hljs-number">6</span> <span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> <span class="hljs-number">6</span> <span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span>
 +-+-+-+-+-------+-+-------------+-------------------------------+
 |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
 |I|S|S|S|  (<span class="hljs-number">4</span>)  |A|     (<span class="hljs-number">7</span>)     |             (<span class="hljs-number">16</span>/<span class="hljs-number">64</span>)           |
 |N|V|V|V|       |S|             |   (<span class="hljs-keyword">if</span> payload len==<span class="hljs-number">126</span>/<span class="hljs-number">127</span>)   |
 | |<span class="hljs-number">1</span>|<span class="hljs-number">2</span>|<span class="hljs-number">3</span>|       |K|             |                               |
 +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
 |     Extended payload length continued, <span class="hljs-keyword">if</span> payload len == <span class="hljs-number">127</span>  |
 + - - - - - - - - - - - - - - - +-------------------------------+
 |                               |Masking-key, <span class="hljs-keyword">if</span> MASK set to <span class="hljs-number">1</span>  |
 +-------------------------------+-------------------------------+
 | Masking-key (continued)       |          Payload Data         |
 +-------------------------------- - - - - - - - - - - - - - - - +
 :                     Payload Data continued ...                :
 + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
 |                     Payload Data continued ...                |
 +---------------------------------------------------------------+
</code></pre>
<ul>
<li>FIN&#xFF1A;1&#x4E2A;&#x6BD4;&#x7279; &#x5982;&#x679C;&#x662F;1&#xFF0C;&#x8868;&#x793A;&#x8FD9;&#x662F;&#x6D88;&#x606F;&#xFF08;message&#xFF09;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5206;&#x7247;&#xFF08;fragment&#xFF09;&#xFF0C;&#x5982;&#x679C;&#x662F;0&#xFF0C;&#x8868;&#x793A;&#x4E0D;&#x662F;&#x662F;&#x6D88;&#x606F;&#xFF08;message&#xFF09;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5206;&#x7247;&#xFF08;fragment&#xFF09;</li>
<li>RSV1, RSV2, RSV3&#xFF1A;&#x5404;&#x5360;1&#x4E2A;&#x6BD4;&#x7279;&#x3002;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#x5168;&#x4E3A;0&#x3002;&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x3001;&#x670D;&#x52A1;&#x7AEF;&#x534F;&#x5546;&#x91C7;&#x7528;WebSocket&#x6269;&#x5C55;&#x65F6;&#xFF0C;&#x8FD9;&#x4E09;&#x4E2A;&#x6807;&#x5FD7;&#x4F4D;&#x53EF;&#x4EE5;&#x975E;0&#xFF0C;&#x4E14;&#x503C;&#x7684;&#x542B;&#x4E49;&#x7531;&#x6269;&#x5C55;&#x8FDB;&#x884C;&#x5B9A;&#x4E49;&#x3002;&#x5982;&#x679C;&#x51FA;&#x73B0;&#x975E;&#x96F6;&#x7684;&#x503C;&#xFF0C;&#x4E14;&#x5E76;&#x6CA1;&#x6709;&#x91C7;&#x7528;WebSocket&#x6269;&#x5C55;&#xFF0C;&#x8FDE;&#x63A5;&#x51FA;&#x9519;&#x3002;</li>
<li>Opcode: 4&#x4E2A;&#x6BD4;&#x7279;&#x3002;&#x64CD;&#x4F5C;&#x4EE3;&#x7801;&#xFF0C;Opcode&#x7684;&#x503C;&#x51B3;&#x5B9A;&#x4E86;&#x5E94;&#x8BE5;&#x5982;&#x4F55;&#x89E3;&#x6790;&#x540E;&#x7EED;&#x7684;&#x6570;&#x636E;&#x8F7D;&#x8377;&#xFF08;data payload&#xFF09;&#x3002;&#x5982;&#x679C;&#x64CD;&#x4F5C;&#x4EE3;&#x7801;&#x662F;&#x4E0D;&#x8BA4;&#x8BC6;&#x7684;&#xFF0C;&#x90A3;&#x4E48;&#x63A5;&#x6536;&#x7AEF;&#x5E94;&#x8BE5;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;&#xFF08;fail the connection&#xFF09;<ul>
<li>%x0&#xFF1A;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x5EF6;&#x7EED;&#x5E27;&#x3002;&#x5F53;Opcode&#x4E3A;0&#x65F6;&#xFF0C;&#x8868;&#x793A;&#x672C;&#x6B21;&#x6570;&#x636E;&#x4F20;&#x8F93;&#x91C7;&#x7528;&#x4E86;&#x6570;&#x636E;&#x5206;&#x7247;&#xFF0C;&#x5F53;&#x524D;&#x6536;&#x5230;&#x7684;&#x6570;&#x636E;&#x5E27;&#x4E3A;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5206;&#x7247;&#x3002;</li>
<li>%x1&#xFF1A;&#x8868;&#x793A;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x6587;&#x672C;&#x5E27;&#xFF08;frame&#xFF09;</li>
<li>%x2&#xFF1A;&#x8868;&#x793A;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x4E8C;&#x8FDB;&#x5236;&#x5E27;&#xFF08;frame&#xFF09;</li>
<li>%x3-7&#xFF1A;&#x4FDD;&#x7559;&#x7684;&#x64CD;&#x4F5C;&#x4EE3;&#x7801;&#xFF0C;&#x7528;&#x4E8E;&#x540E;&#x7EED;&#x5B9A;&#x4E49;&#x7684;&#x975E;&#x63A7;&#x5236;&#x5E27;&#x3002;</li>
<li>%x8&#xFF1A;&#x8868;&#x793A;&#x8FDE;&#x63A5;&#x65AD;&#x5F00;&#x3002;</li>
<li>%x9&#xFF1A;&#x8868;&#x793A;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;ping&#x64CD;&#x4F5C;&#x3002;</li>
<li>%xA&#xFF1A;&#x8868;&#x793A;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;pong&#x64CD;&#x4F5C;&#x3002;</li>
<li>%xB-F&#xFF1A;&#x4FDD;&#x7559;&#x7684;&#x64CD;&#x4F5C;&#x4EE3;&#x7801;&#xFF0C;&#x7528;&#x4E8E;&#x540E;&#x7EED;&#x5B9A;&#x4E49;&#x7684;&#x63A7;&#x5236;&#x5E27;&#x3002;</li>
</ul>
</li>
<li>Mask: 1&#x4E2A;&#x6BD4;&#x7279;&#x3002;&#x8868;&#x793A;&#x662F;&#x5426;&#x8981;&#x5BF9;&#x6570;&#x636E;&#x8F7D;&#x8377;&#x8FDB;&#x884C;&#x63A9;&#x7801;&#x64CD;&#x4F5C;<ul>
<li>&#x4ECE;&#x5BA2;&#x6237;&#x7AEF;&#x5411;&#x670D;&#x52A1;&#x7AEF;&#x53D1;&#x9001;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x63A9;&#x7801;&#x64CD;&#x4F5C;&#xFF1B;&#x4ECE;&#x670D;&#x52A1;&#x7AEF;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x63A9;&#x7801;&#x64CD;&#x4F5C;,&#x5982;&#x679C;&#x670D;&#x52A1;&#x7AEF;&#x63A5;&#x6536;&#x5230;&#x7684;&#x6570;&#x636E;&#x6CA1;&#x6709;&#x8FDB;&#x884C;&#x8FC7;&#x63A9;&#x7801;&#x64CD;&#x4F5C;&#xFF0C;&#x670D;&#x52A1;&#x7AEF;&#x9700;&#x8981;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;&#x3002;</li>
<li>&#x5982;&#x679C;Mask&#x662F;1&#xFF0C;&#x90A3;&#x4E48;&#x5728;Masking-key&#x4E2D;&#x4F1A;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x63A9;&#x7801;&#x952E;&#xFF08;masking key&#xFF09;&#xFF0C;&#x5E76;&#x7528;&#x8FD9;&#x4E2A;&#x63A9;&#x7801;&#x952E;&#x6765;&#x5BF9;&#x6570;&#x636E;&#x8F7D;&#x8377;&#x8FDB;&#x884C;&#x53CD;&#x63A9;&#x7801;&#x3002;&#x6240;&#x6709;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x5230;&#x670D;&#x52A1;&#x7AEF;&#x7684;&#x6570;&#x636E;&#x5E27;&#xFF0C;Mask&#x90FD;&#x662F;1&#x3002;</li>
</ul>
</li>
<li>Payload length&#xFF1A;&#x6570;&#x636E;&#x8F7D;&#x8377;&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x5355;&#x4F4D;&#x662F;&#x5B57;&#x8282;&#x3002;&#x4E3A;7&#x4F4D;&#xFF0C;&#x6216;7+16&#x4F4D;&#xFF0C;&#x6216;7+64&#x4F4D;&#x3002;<ul>
<li>Payload length=x&#x4E3A;0~125&#xFF1A;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;&#x4E3A;x&#x5B57;&#x8282;&#x3002;</li>
<li>Payload length=x&#x4E3A;126&#xFF1A;&#x540E;&#x7EED;2&#x4E2A;&#x5B57;&#x8282;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;16&#x4F4D;&#x7684;&#x65E0;&#x7B26;&#x53F7;&#x6574;&#x6570;&#xFF0C;&#x8BE5;&#x65E0;&#x7B26;&#x53F7;&#x6574;&#x6570;&#x7684;&#x503C;&#x4E3A;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;&#x3002;</li>
<li>Payload length=x&#x4E3A;127&#xFF1A;&#x540E;&#x7EED;8&#x4E2A;&#x5B57;&#x8282;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;64&#x4F4D;&#x7684;&#x65E0;&#x7B26;&#x53F7;&#x6574;&#x6570;&#xFF08;&#x6700;&#x9AD8;&#x4F4D;&#x4E3A;0&#xFF09;&#xFF0C;&#x8BE5;&#x65E0;&#x7B26;&#x53F7;&#x6574;&#x6570;&#x7684;&#x503C;&#x4E3A;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;&#x3002; </li>
<li>&#x5982;&#x679C;payload length&#x5360;&#x7528;&#x4E86;&#x591A;&#x4E2A;&#x5B57;&#x8282;&#x7684;&#x8BDD;&#xFF0C;payload length&#x7684;&#x4E8C;&#x8FDB;&#x5236;&#x8868;&#x8FBE;&#x91C7;&#x7528;&#x7F51;&#x7EDC;&#x5E8F;&#xFF08;big endian&#xFF0C;&#x91CD;&#x8981;&#x7684;&#x4F4D;&#x5728;&#x524D;)</li>
</ul>
</li>
<li>Masking-key&#xFF1A;0&#x6216;4&#x5B57;&#x8282;(32&#x4F4D;) &#x6240;&#x6709;&#x4ECE;&#x5BA2;&#x6237;&#x7AEF;&#x4F20;&#x9001;&#x5230;&#x670D;&#x52A1;&#x7AEF;&#x7684;&#x6570;&#x636E;&#x5E27;&#xFF0C;&#x6570;&#x636E;&#x8F7D;&#x8377;&#x90FD;&#x8FDB;&#x884C;&#x4E86;&#x63A9;&#x7801;&#x64CD;&#x4F5C;&#xFF0C;Mask&#x4E3A;1&#xFF0C;&#x4E14;&#x643A;&#x5E26;&#x4E86;4&#x5B57;&#x8282;&#x7684;Masking-key&#x3002;&#x5982;&#x679C;Mask&#x4E3A;0&#xFF0C;&#x5219;&#x6CA1;&#x6709;Masking-key&#x3002;&#x8F7D;&#x8377;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x4E0D;&#x5305;&#x62EC;mask key&#x7684;&#x957F;&#x5EA6;</li>
<li>Payload data&#xFF1A;(x+y) &#x5B57;&#x8282;<ul>
<li>&#x8F7D;&#x8377;&#x6570;&#x636E;&#xFF1A;&#x5305;&#x62EC;&#x4E86;&#x6269;&#x5C55;&#x6570;&#x636E;&#x3001;&#x5E94;&#x7528;&#x6570;&#x636E;&#x3002;&#x5176;&#x4E2D;&#xFF0C;&#x6269;&#x5C55;&#x6570;&#x636E;x&#x5B57;&#x8282;&#xFF0C;&#x5E94;&#x7528;&#x6570;&#x636E;y&#x5B57;&#x8282;&#x3002;</li>
<li>&#x6269;&#x5C55;&#x6570;&#x636E;&#xFF1A;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x534F;&#x5546;&#x4F7F;&#x7528;&#x6269;&#x5C55;&#x7684;&#x8BDD;&#xFF0C;&#x6269;&#x5C55;&#x6570;&#x636E;&#x6570;&#x636E;&#x4E3A;0&#x5B57;&#x8282;&#x3002;&#x6240;&#x6709;&#x7684;&#x6269;&#x5C55;&#x90FD;&#x5FC5;&#x987B;&#x58F0;&#x660E;&#x6269;&#x5C55;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x6216;&#x8005;&#x53EF;&#x4EE5;&#x5982;&#x4F55;&#x8BA1;&#x7B97;&#x51FA;&#x6269;&#x5C55;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x6269;&#x5C55;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x5FC5;&#x987B;&#x5728;&#x63E1;&#x624B;&#x9636;&#x6BB5;&#x5C31;&#x534F;&#x5546;&#x597D;&#x3002;&#x5982;&#x679C;&#x6269;&#x5C55;&#x6570;&#x636E;&#x5B58;&#x5728;&#xFF0C;&#x90A3;&#x4E48;&#x8F7D;&#x8377;&#x6570;&#x636E;&#x957F;&#x5EA6;&#x5FC5;&#x987B;&#x5C06;&#x6269;&#x5C55;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;&#x5305;&#x542B;&#x5728;&#x5185;&#x3002;</li>
<li>&#x5E94;&#x7528;&#x6570;&#x636E;&#xFF1A;&#x4EFB;&#x610F;&#x7684;&#x5E94;&#x7528;&#x6570;&#x636E;&#xFF0C;&#x5728;&#x6269;&#x5C55;&#x6570;&#x636E;&#x4E4B;&#x540E;&#xFF08;&#x5982;&#x679C;&#x5B58;&#x5728;&#x6269;&#x5C55;&#x6570;&#x636E;&#xFF09;&#xFF0C;&#x5360;&#x636E;&#x4E86;&#x6570;&#x636E;&#x5E27;&#x5269;&#x4F59;&#x7684;&#x4F4D;&#x7F6E;&#x3002;&#x8F7D;&#x8377;&#x6570;&#x636E;&#x957F;&#x5EA6; &#x51CF;&#x53BB; &#x6269;&#x5C55;&#x6570;&#x636E;&#x957F;&#x5EA6;&#xFF0C;&#x5C31;&#x5F97;&#x5230;&#x5E94;&#x7528;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;&#x3002;</li>
</ul>
</li>
</ul>
<h4 id="t182.4.2 &#x63A9;&#x7801;&#x7B97;&#x6CD5;">2.4.2 &#x63A9;&#x7801;&#x7B97;&#x6CD5; <a href="#t182.4.2 &#x63A9;&#x7801;&#x7B97;&#x6CD5;"> # </a></h4>
<p>&#x63A9;&#x7801;&#x952E;&#xFF08;Masking-key&#xFF09;&#x662F;&#x7531;&#x5BA2;&#x6237;&#x7AEF;&#x6311;&#x9009;&#x51FA;&#x6765;&#x7684;32&#x4F4D;&#x7684;&#x968F;&#x673A;&#x6570;&#x3002;&#x63A9;&#x7801;&#x64CD;&#x4F5C;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x6570;&#x636E;&#x8F7D;&#x8377;&#x7684;&#x957F;&#x5EA6;&#x3002;&#x63A9;&#x7801;&#x3001;&#x53CD;&#x63A9;&#x7801;&#x64CD;&#x4F5C;&#x90FD;&#x91C7;&#x7528;&#x5982;&#x4E0B;&#x7B97;&#x6CD5;&#xFF1A;</p>
<ul>
<li>&#x5BF9;&#x7D22;&#x5F15;i&#x6A21;&#x4EE5;4&#x5F97;&#x5230;j,&#x56E0;&#x4E3A;&#x63A9;&#x7801;&#x4E00;&#x5171;&#x5C31;&#x662F;&#x56DB;&#x4E2A;&#x5B57;&#x8282;</li>
<li>&#x5BF9;&#x539F;&#x6765;&#x7684;&#x7D22;&#x5F15;&#x8FDB;&#x884C;&#x5F02;&#x6216;&#x5BF9;&#x5E94;&#x7684;&#x63A9;&#x7801;&#x5B57;&#x8282;</li>
<li>&#x5F02;&#x6216;&#x5C31;&#x662F;&#x4E24;&#x4E2A;&#x6570;&#x7684;&#x4E8C;&#x8FDB;&#x5236;&#x5F62;&#x5F0F;&#xFF0C;&#x6309;&#x4F4D;&#x5BF9;&#x6BD4;&#xFF0C;&#x76F8;&#x540C;&#x53D6;0&#xFF0C;&#x4E0D;&#x540C;&#x53D6;1<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">unmask</span>(<span class="hljs-params">buffer, mask</span>) </span>{
      <span class="hljs-keyword">const</span> length = buffer.length;
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; length; i++) {
          buffer[i] ^= mask[i &amp; <span class="hljs-number">3</span>];
      }
  }
</code></pre>
</li>
</ul>
<h4 id="t192.4.3 &#x670D;&#x52A1;&#x5668;&#x5B9E;&#x6218;">2.4.3 &#x670D;&#x52A1;&#x5668;&#x5B9E;&#x6218; <a href="#t192.4.3 &#x670D;&#x52A1;&#x5668;&#x5B9E;&#x6218;"> # </a></h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>);
<span class="hljs-keyword">const</span> CODE = <span class="hljs-string">&apos;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&apos;</span>;
<span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    socket.once(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        data = data.toString();
        <span class="hljs-keyword">if</span> (data.match(<span class="hljs-regexp">/Upgrade: websocket/</span>)) {
            <span class="hljs-keyword">let</span> rows = data.split(<span class="hljs-string">&apos;\r\n&apos;</span>);<span class="hljs-comment">//&#x6309;&#x5206;&#x5272;&#x7B26;&#x5206;&#x5F00;</span>
            rows = rows.slice(<span class="hljs-number">1</span>, <span class="hljs-number">-2</span>);<span class="hljs-comment">//&#x53BB;&#x6389;&#x8BF7;&#x6C42;&#x884C;&#x548C;&#x5C3E;&#x90E8;&#x7684;&#x4E8C;&#x4E2A;&#x5206;&#x9694;&#x7B26;</span>
            <span class="hljs-keyword">const</span> headers = {};
            rows.forEach(<span class="hljs-function"><span class="hljs-params">row</span> =&gt;</span> {
                <span class="hljs-keyword">let</span> [key, value] = row.split(<span class="hljs-string">&apos;: &apos;</span>);
                headers[key] = value;
            });
            <span class="hljs-keyword">if</span> (headers[<span class="hljs-string">&apos;Sec-WebSocket-Version&apos;</span>] == <span class="hljs-number">13</span>) {
                <span class="hljs-keyword">let</span> wsKey = headers[<span class="hljs-string">&apos;Sec-WebSocket-Key&apos;</span>];
                <span class="hljs-keyword">let</span> acceptKey = crypto.createHash(<span class="hljs-string">&apos;sha1&apos;</span>).update(wsKey + CODE).digest(<span class="hljs-string">&apos;base64&apos;</span>);
                <span class="hljs-keyword">let</span> response = [
                    <span class="hljs-string">&apos;HTTP/1.1 101 Switching Protocols&apos;</span>,
                    <span class="hljs-string">&apos;Upgrade: websocket&apos;</span>,
                    <span class="hljs-string">`Sec-WebSocket-Accept: <span class="hljs-subst">${acceptKey}</span>`</span>,
                    <span class="hljs-string">&apos;Connection: Upgrade&apos;</span>,
                    <span class="hljs-string">&apos;\r\n&apos;</span>
                ].join(<span class="hljs-string">&apos;\r\n&apos;</span>);
                socket.write(response);
                socket.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">buffers</span>) </span>{
                    <span class="hljs-keyword">let</span> _fin = (buffers[<span class="hljs-number">0</span>] &amp; <span class="hljs-number">0b10000000</span>) === <span class="hljs-number">0b10000000</span>;<span class="hljs-comment">//&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;&#x7ED3;&#x675F;&#x4F4D;,&#x7B2C;&#x4E00;&#x4E2A;bit&#x662F;&#x4E0D;&#x662F;1</span>
                    <span class="hljs-keyword">let</span> _opcode = buffers[<span class="hljs-number">0</span>] &amp; <span class="hljs-number">0b00001111</span>;<span class="hljs-comment">//&#x53D6;&#x4E00;&#x4E2A;&#x5B57;&#x8282;&#x7684;&#x540E;&#x56DB;&#x4F4D;,&#x5F97;&#x5230;&#x7684;&#x4E00;&#x4E2A;&#x662F;&#x5341;&#x8FDB;&#x5236;&#x6570;</span>
                    <span class="hljs-keyword">let</span> _masked = buffers[<span class="hljs-number">1</span>] &amp; <span class="hljs-number">0b100000000</span> === <span class="hljs-number">0b100000000</span>;<span class="hljs-comment">//&#x7B2C;&#x4E00;&#x4F4D;&#x662F;&#x5426;&#x662F;1</span>
                    <span class="hljs-keyword">let</span> _payloadLength = buffers[<span class="hljs-number">1</span>] &amp; <span class="hljs-number">0b01111111</span>;<span class="hljs-comment">//&#x53D6;&#x5F97;&#x8D1F;&#x8F7D;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;</span>
                    <span class="hljs-keyword">let</span> _mask = buffers.slice(<span class="hljs-number">2</span>, <span class="hljs-number">6</span>);<span class="hljs-comment">//&#x63A9;&#x7801;</span>
                    <span class="hljs-keyword">let</span> payload = buffers.slice(<span class="hljs-number">6</span>);<span class="hljs-comment">//&#x8D1F;&#x8F7D;&#x6570;&#x636E;</span>

                    unmask(payload, _mask);<span class="hljs-comment">//&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x89E3;&#x7801;&#x5904;&#x7406;</span>

                    <span class="hljs-comment">//&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x54CD;&#x5E94;&#x6570;&#x636E;</span>
                    <span class="hljs-keyword">let</span> response = Buffer.alloc(<span class="hljs-number">2</span> + payload.length);
                    response[<span class="hljs-number">0</span>] = _opcode | <span class="hljs-number">0b10000000</span>;<span class="hljs-comment">//1&#x8868;&#x793A;&#x53D1;&#x9001;&#x7ED3;&#x675F;</span>
                    response[<span class="hljs-number">1</span>] = payload.length;<span class="hljs-comment">//&#x8D1F;&#x8F7D;&#x7684;&#x957F;&#x5EA6;</span>
                    payload.copy(response, <span class="hljs-number">2</span>);
                    socket.write(response);
                });
            }

        }
    });
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">unmask</span>(<span class="hljs-params">buffer, mask</span>) </span>{
        <span class="hljs-keyword">const</span> length = buffer.length;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; length; i++) {
            buffer[i] ^= mask[i &amp; <span class="hljs-number">3</span>];
        }
    }
    socket.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;end&apos;</span>);
    });
    socket.on(<span class="hljs-string">&apos;close&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;close&apos;</span>);
    });
    socket.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
        <span class="hljs-built_in">console</span>.log(error);
    });
});
server.listen(<span class="hljs-number">9999</span>);
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
